
* REPLICATION CODE: JENNINGS & WLEZIEN, 'THE COMPARATIVE TIMELINE OF ELECTIONS', AMERICAN JOURNAL OF POLITICAL SCIENCE *

* FIGURE S11. Root Mean Squared Errors (US, UK and Germany), Legislative Elections in Proportional vs. Plurality Systems, Vote = Polls

*********************************************************************************************
** MULTIPLE IMPUTATION/BOOTSTRAP - U.S., U.K., & GERMANY - VOTE = POLLS *********************
*********************************************************************************************

use MI_COVAR_ALL.dta, clear

keep if election=="Legislative"
keep if rule=="PR"

gen rmse=.
gen se_rmse=.

foreach x of numlist 1/200 {
quietly mi estimate, cmdok: mybstrprmseareg vote_ poll_ if daysbeforeED==`x' & ipoll!=. & imp_!=. & iics_!=. 
matrix bts = e(b_mi)
scalar bt = bts[1,1]
replace rmse=bt in `x'
matrix bts = e(V_mi)
scalar bt = bts[1,1]
replace se_rmse=sqrt(bt) in `x'
ereturn clear
}

*********************************************************************************************
** OUTPUT/FORMAT THE TIMELINE DATASET *******************************************************
*********************************************************************************************

keep rmse se_rmse
gen daysbeforeED=_n

drop if rmse == .
mi xtset, clear 
sort daysbeforeED

gen rmse_min95=rmse+se_rmse
gen rmse_max95=rmse-se_rmse

save MI_LEG_PR_RMSE_EQ1.dta, replace

*********************************************************************************************
** MULTIPLE IMPUTATION/BOOTSTRAP - U.S., U.K., & GERMANY - VOTE = POLLS *********************
*********************************************************************************************

use MI_COVAR_ALL.dta, clear

keep if election=="Legislative"
keep if rule=="SMDP"

gen rmse=.
gen se_rmse=.

foreach x of numlist 1/200 {
quietly mi estimate, cmdok: mybstrprmseareg vote_ poll_ if daysbeforeED==`x' & ipoll!=. & imp_!=. & iics_!=. 
matrix bts = e(b_mi)
scalar bt = bts[1,1]
replace rmse=bt in `x'
matrix bts = e(V_mi)
scalar bt = bts[1,1]
replace se_rmse=sqrt(bt) in `x'
ereturn clear
}

*********************************************************************************************
** OUTPUT/FORMAT THE TIMELINE DATASET *******************************************************
*********************************************************************************************

keep rmse se_rmse
gen daysbeforeED=_n

drop if rmse == .
mi xtset, clear 
sort daysbeforeED

gen rmse_min95=rmse+se_rmse
gen rmse_max95=rmse-se_rmse

save MI_LEG_SMD_RMSE_EQ1.dta, replace

*******************************************************************************
** LEGISLATIVE ELECTIONS IN PR vs. SMDP SYSTEMS *******************************
*******************************************************************************

use MI_LEG_SMD_RMSE_EQ1.dta, clear
rename rmse leg_smd_eqn1_rmse
rename rmse_min95 leg_smd_eqn1_rmse_min95
rename rmse_max95 leg_smd_eqn1_rmse_max95
drop se_rmse
sort daysbeforeED
save MI_LEG_SMD_RMSE_EQ1.dta, replace

use MI_LEG_PR_RMSE_EQ1.dta, clear
rename rmse leg_pr_eqn1_rmse
rename rmse_min95 leg_pr_eqn1_rmse_min95
rename rmse_max95 leg_pr_eqn1_rmse_max95
drop se_rmse
sort daysbeforeED
save MI_LEG_PR_RMSE_EQ1.dta, replace

use MI_LEG_SMD_RMSE_EQ1.dta, clear
merge daysbeforeED using MI_LEG_PR_RMSE_EQ1.dta
drop _merge
sort daysbeforeED

* 
twoway /*
*/  || line leg_pr_eqn1_rmse daysbeforeED,               /*
*/  clpattern(solid) clcolor(black) clwidth(medthick)    /* 
*/  || line leg_smd_eqn1_rmse daysbeforeED,              /*
*/  clpattern(dash) clcolor(black) clwidth(medthick)     /* 
*/  || line leg_pr_eqn1_rmse_min95 daysbeforeED,         /*
*/  clpattern(dot) clcolor(black) clwidth(medium)        /*
*/  || line leg_pr_eqn1_rmse_max95 daysbeforeED,         /*
*/  clpattern(dot) clcolor(black) clwidth(medium)        /*
*/  || line leg_smd_eqn1_rmse_min95 daysbeforeED,        /*
*/  clpattern(dot) clcolor(black) clwidth(medium)        /*
*/  || line leg_smd_eqn1_rmse_max95 daysbeforeED,        /*
*/  clpattern(dot) clcolor(black) clwidth(medium)        /*
*/  , scheme(s2mono) graphregion(color(white))           /*
*/  xtitle("Days Until Election")                        /*
*/  title("")                                            /*
*/  ylabel(0(1)8, gmax angle(horizontal))                /*
*/  ytick(0(1)8)                                         /*
*/  ytitle(Root Mean Squared Error)                      /*
*/  xlabel(0(50)200)                                     /*
*/  xscale(reverse) xscale(titlegap(2))                  /*
*/  legend(order(1 "PR" 2 "SMD")                         /*
*/  rows(1)                                              /*
*/  size(medsmall))

graph export FigS11.tif
